package com.recommend.controller.movie;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.recommend.domain.CommentDomain;
import com.recommend.domain.EvaluateDomain;
import com.recommend.domain.MovieDomain;
import com.recommend.dto.admin.Comment;
import com.recommend.dto.admin.CommentEvaluateDto;
import com.recommend.service.CommentService;
import com.recommend.service.EvaluateService;
import com.recommend.service.MovieService;
import com.recommend.service.Customer.CustomerService;
@Controller
public class ShowMovieController {
	
	
	@Autowired
    private MovieService movieService;
	
	@Autowired 
	private CommentService commentService;
	@Resource
    private CustomerService customerService;
	@Autowired
    private EvaluateService evaluateService;
	/**
	 * 进入主界面
	 * @return
	 */
	@RequestMapping({ "/", "/index" })
	public ModelAndView home(){
		ModelAndView modelAndView = new ModelAndView("index");
		return modelAndView;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public PageInfo<MovieDomain> list(@RequestParam(value = "page",defaultValue="0",required = false)Integer page,
			@RequestParam(value = "size",defaultValue="8",required = false)Integer size){
		PageHelper.startPage(page, size);// 初始化分页
		List<MovieDomain> movies = movieService.getAllMovie();
		PageInfo<MovieDomain> pageInfo = new PageInfo<MovieDomain>(movies);
		return pageInfo;
	}
	
	/**
	 * 电影详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail/{id}")
	@ResponseBody
	public ModelAndView detail(@PathVariable("id") Integer id){
		ModelAndView modelAndView =new ModelAndView("single");
		MovieDomain movie = movieService.getMovie(id);
		modelAndView.addObject("movie", movie);
		List<Comment> comments = commentService.selectCommentsByMovie(id);
		modelAndView.addObject("comments", comments);
		return modelAndView;
	}
	
    @RequestMapping(value = "/detail/insertCommentEvaluate", method = RequestMethod.POST)
    @ResponseBody
    public void insertCommentEvaluate(CommentEvaluateDto commentEvaluateDto){
        CommentDomain commentDomain = new CommentDomain();
        commentDomain.setCustomerId(commentEvaluateDto.getCustomerId());
        commentDomain.setMovieId(commentEvaluateDto.getMovieId());
        commentDomain.setContent(commentEvaluateDto.getContent());
        commentService.insertComment(commentDomain);

        EvaluateDomain evaluateDomain = new EvaluateDomain();
        evaluateDomain.setCustomerId(commentEvaluateDto.getCustomerId());
        evaluateDomain.setMovieId(commentEvaluateDto.getMovieId());
        evaluateDomain.setRating(commentEvaluateDto.getRating());
        evaluateService.insertEvaluate(evaluateDomain);
    }
}
